package com.mallcai.bigdata.ladon.dpl.mapper.system.provider;

import com.mallcai.bigdata.ladon.dpl.entity.system.User;
import com.mallcai.bigdata.ladon.dpl.mapper.BaseSqlProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
public class UserSessionSqlProvider extends BaseSqlProvider<User> {
    public static final String TB_USER_SESSION = "sys_user_session";


    @Override
    public String getTableName() {
        return TB_USER_SESSION;
    }

    @Override
    public Map<String, String> getInsertColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("user_id", "#{userId}");
        map.put("token", "#{token}");
        map.put("login_time", "#{loginTime}");
        map.put("timeout", "#{timeout}");
        return map;
    }

    @Override
    public Map<String, String> getUpdateColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("userId", "user_id");
        map.put("token", "token");
        map.put("loginTime", "login_time");
        map.put("timeout", "timeout");
        return map;
    }


    /**
     * 删除过期token信息
     */
    public String deleteExpireToken() {
        return new SQL() {
            {
                DELETE_FROM(getTableName());
                WHERE("(login_time + timeout * 60 * 1000) < unix_timestamp(now()) * 1000");
            }
        }.toString();
    }
}
